// 引入Sequelize模块
const Sequelize = require('sequelize');
const user = require('./user');
const artPPT = require('./artPPT');

// 引入数据库实例
const db = require('../db');
// 定义model
/***banner管理 */
const branner = db.define('branner', {
    objectId: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true,
        primaryKey: true
    },

    image: {
        type: Sequelize.STRING(200),
    },

    title: {
        type: Sequelize.STRING(100),
    },

    artpptId: {
        type: Sequelize.STRING(20),
    },

    isShow: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
    },

    createdAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd');
        }
    },

    updatedAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd');
        }
    },
}, {
    // 是否支持驼峰
    underscored: false,
    // MySQL数据库表名
    tableName: 'branner',
});

branner.belongsTo(artPPT, {
    foreignKey: 'artpptId',
    as: 'artPPT',
    onDelete: 'SET NULL',
});

/*(async () => {
    await branner.sync({
        alter: true,
    });
    console.log("branner字段同步成功！");
})();*/

/**end */


/***日签图管理 */
const todayBranner = db.define('todayBranner', {
    objectId: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true,
        primaryKey: true
    },

    time: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('time');
            return dateFormat(rawValue, 'yyyy-mm-dd');
        }
    },

    image: {
        type: Sequelize.STRING(200),
    },

    title: {
        type: Sequelize.STRING(100),
    },

    artpptId: {
        type: Sequelize.STRING(20),
    },

    createdAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd');
        }
    },

    updatedAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('updatedAt');
            return dateFormat(rawValue, 'yyyy-mm-dd');
        }
    },
}, {
    // 是否支持驼峰
    underscored: false,
    // MySQL数据库表名
    tableName: 'todayBranner',
});

todayBranner.belongsTo(artPPT, {
    foreignKey: 'artpptId',
    as: 'artPPT',
    onDelete: 'SET NULL',
});

/*(async () => {
    await todayBranner.sync({
        alter: true,
    });
    console.log("todayBranner字段同步成功！");
})();*/

/**end */


/***日签管理 */
const todayQd = db.define('todayQd', {
    objectId: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true,
        primaryKey: true
    },

    bannerId: {
        type: Sequelize.STRING(20),
    },

    userId: {
        type: Sequelize.STRING(20),
    },

    createdAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd');
        }
    },

    updatedAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd');
        }
    },
}, {
    // 是否支持驼峰
    underscored: false,
    // MySQL数据库表名
    tableName: 'todayQd',
});

/*(async () => {
    await todayQd.sync({
        alter: true,
    });
    console.log("todayQd字段同步成功！");
})();

/**end */


module.exports = {
    branner,
    todayBranner,
    todayQd
};
